#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QItemSelectionModel>
#include <QHeaderView>
#include <QLabel>
#include <QPalette>
#include <QtSql>
#include <QDateTime>
#include "tbuttondelegate.h"
#include "tcheckboxdelegate.h"
#include <QMessageBox>
#include "tserialport.h"
#include <QFile>
#include <QUrl>
#include <QDesktopServices>
#include "tupdatedialog.h"
#include <QFileDialog>
#include "tfilesave.h"
#include <QSignalMapper>
#include <QMap>
#include "smtp.h"
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QRandomGenerator>
#include "tlog.h"
#include "tsqlrelationaldelegate.h"
#include "ttexteditdelegate.h"
#include "tsqlrelationaltablemodel.h"
#include "tscriptdelegate.h"

#include "xlsxdocument.h"
#include "xlsxchartsheet.h"
#include "xlsxcellrange.h"
#include "xlsxchart.h"
#include "xlsxrichstring.h"
#include "xlsxworkbook.h"

#define MAXLOGLENGTH 102400
#define MAXCACHELENGTH MAXLOGLENGTH+10240
#define TICKOUT 50

#define LOCAL_USER_ID 1

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow;}
QT_END_NAMESPACE


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void logRAW(int cate,QString fileName,QString func,int line,QString info);
#define log(cate,info) logRAW( cate, __FILE__, Q_FUNC_INFO, __LINE__, info)
#define INFO 0
#define WARNING 1
#define ERROR 2
private:
    bool createConnection();
    void initRelationalTables();
    void initSqlModel();
    bool createModel(QString case_name);
    bool deleteModel(QString case_name);
    bool changedRelationalTables(QString model_name);
    bool refreshRelationalTables();
    void refreshCase();
    void uartSend(QByteArray bts,QByteArray btsB="");
    void refreshTableView();
    void comboBox_widthAdjust(QComboBox *cb);

private slots:
    void on_btnSerialRefresh_clicked();

    void on_btnClear_clicked();

    void on_btnSerialSend_clicked();

    void on_chkBoxAddReturn_clicked(bool checked);

    void on_btnInsertCell_clicked();

    void on_btnAddCell_clicked();

    void on_btnRemoveCell_clicked();

    void on_itemClick(int row); //代理按钮被点击所调用的槽函数

    void on_itemHexClicked(QAbstractItemModel *model,const QModelIndex &index);

    void on_chkBoxAddTimeStamp_clicked(bool checked);
    void on_serialStateChanged(const int state,const QString portName,const QString info);
    void on_serialRevShowA(QByteArray readBuf);
    void on_serialRevShowB(QByteArray readBuf);
    void on_btnSaveLog_clicked();

    void on_chkBoxTimerSend_clicked(bool checked);

    void on_btnSendNbr_clicked();
    void on_timerSendNbr();
    void on_timerSendCaseListTest();
    void on_chkBoxSendCircle_clicked(bool checked);

    void on_chkBoxSendHex_clicked(bool checked);

    void on_btnAddTables_clicked();

    void on_btnDeleteTables_clicked();

    void on_btnRefreshTables_clicked();
    void on_menuHelpTriggered(QAction *action);
    void on_menuProductTriggered(QAction *action);
    void on_btnOpenFile_clicked();
    void on_btnSendFile_clicked();
    void on_btnOpenFile_2_clicked();
    void on_btnSendFile_2_clicked();
    void on_btnOpenFile_3_clicked();
    void on_btnSendFile_3_clicked();
    void on_btnOpenFile_4_clicked();
    void on_btnSendFile_4_clicked();

    void on_btnOpenCompareTools_clicked();

    void on_btnSerialOpenA_clicked();
    void on_btnSerialOpenB_clicked();

    void on_comboBoxCompare_activated(int index);

    void on_comboBoxTablesCase_activated(int index);

    void on_comboBoxTablesModel_activated(int index);

    void on_comboBoxTablesFunc_activated(int index);

    void on_btnRenameTables_clicked();

    void on_chkBoxAddRxTx_clicked(bool checked);

    void on_btnMoveDown_clicked();

    void on_btnMoveUp_clicked();

    void on_btnImportCaseXlsx_clicked();

    void on_btnExportCaseXlsx_clicked();

    void on_chkBoxCaseReport_stateChanged(int arg1);

    void on_btnImportCaseListXlsx_clicked();

    void on_btnClearCaseList_clicked();

    void on_btnStartCaseTest_clicked();

signals:
    void saveFile(QByteArray ar,QString fileName);
    void savelog(int cate,QString time,QString fileName,QString func,int line,QString info);

private:
    Ui::MainWindow *ui;
    TupdateDialog *updateDialog;

    bool addReturnFlag=false;

    bool addTimeStampFlag=false;
    bool addRxTxFlag=false;



    QString storePlainTextHexA;
    QString storePlainTextHexB;


    QByteArray sendTextA;
    QByteArray recvTextA;
    QByteArray sendTextB;
    QByteArray recvTextB;

    QByteArray logA;
    QByteArray logB;

    QByteArray expectReturn;
    QByteArray realReturn;
    QModelIndex curIndex;


    int TxCntA=0;
    int RxCntA=0;
    int TxCntB=0;
    int RxCntB=0;
    int circleCnt=0;
    int timerCnt=0;

    QStandardItemModel *m_model;

    QLabel *labelTip;
    QLabel *labelInfoSql;
    QLabel *labelSeiralInfoA;
    QLabel *labelSeiralInfoB;
    QLabel *labelSeiralSendCntA;
    QLabel *labelSeiralSendCntB;
    QLabel *labelSeiralRevCntA;
    QLabel *labelSeiralRevCntB;
    QLabel *labelLogInUser;
    TSqlRelationalTableModel *tabModel;
    QItemSelectionModel *selModel;
    TButtonDelegate     *m_delegate;    //按钮代理指针
    TCheckBoxDelegate     *chkboxdelegate;    //按钮代理指针

    QDateTime *dateTime;

    TserialPort *serialA;
    TserialPort *serialB;
    QString portNameA;
    QString portNameB;
    QSerialPort::BaudRate baudRate;
    QSerialPort::DataBits dataBits;
    QSerialPort::Parity parity;
    QSerialPort::StopBits stopBits;
    bool serialStateA=false;
    bool serialStateB=false;
    bool hexSendFlag=false;
    bool timerSendFlag=false;

    QMutex *mutex;
    QMessageBox *messageBox;
    QTimer *timerSend;
    QTimer *timerSendNbr;
    qint64 curTimerSendNbr;


    int rowCnt;
    int rowNbr;
    QVariantMap scriptJson;
    bool rowCricle=false;

    //测试报告
    bool rowCaseTestList=false;
    QXlsx::Document *xlsxDoc;
    int rowNbrCur;


    QSqlRelationalDelegate *sqlRelationalDelegate;
    int curDataBase=0;
    bool login=false;
    QSqlDatabase sqlitedb;
    QMap<QString,int> modelMap;
    QMap<QString,int> funcMap;
    QMap<QString,int> caseMap;
    int mysqlUserID=0;
    QString mysqlUserName;

    SMTP *foxMail;
    QTimer *verificationTimer;
    quint32 verificationCode=0;
    quint32 verificationTimerCnt=0;
    bool logenable=false;
};
#endif // MAINWINDOW_H
